package parseIP;

import city.City;
import com.alibaba.fastjson.JSON;
import database.dbutlis.JDBCUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 传入IP地址解析url
 */
public class ParseIP {
    //传入IP地址,请求三方数据,解析IP归属地
    public String parseUrl(String ip) throws IOException {
        City city = new City();
        Map<String,Object> map = new HashMap<String, Object>();
        Document document = Jsoup.connect("http://www.ip138.com/ips138.asp?ip="+ip+"&action=2")
                .timeout(5000).get();
        Element element = document.select(".ul1 li").first();
        String text = element.text();//本站数据：山东省淄博市  联通
        String network = text.substring(text.indexOf("：")+1);
        city.setIP(ip);
        city.setNetwork(network);
        map.put(ip,city);
        String string = JSON.toJSONString(map);
        System.out.println(string);
        return string;
    }

    public static void main(String[] args) throws IOException {
        ParseIP parseIP  = new ParseIP();
        String string = parseIP.parseUrl("27.195.157.210");
        System.out.println(string);
        JDBCUtil.save(string);
    }
}
